package torres.map;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Torres
 * A singleton that contains all of the Maps.
 */
@SuppressWarnings("serial")
public class World implements Serializable{
	private static final World instance = new World();
	private Map currentMap;
	private List<Map> mapList;
	
	/**
	 * Creates a new World with an empty list of Maps.
	 */
	private World() {
		mapList = new ArrayList<Map>();
	}
	
	
	/**
	 * Returns the single instance of World.
	 * @return The single instance of World.
	 */
	public static World getInstance() {
		
		return instance;
	}
	
	/**
	 * Adds the specified Map to World's list of maps.
	 * @param map The Map to be added to the map list.
	 */
	public void addMap(Map map) {
		
		if(!mapList.contains(map)) {
			
			mapList.add(map);
		}
	}
	
	/**
	 * Returns the active Map.
	 * @return The active Map.
	 */
	public Map getCurrentMap() {
		
		return currentMap;
	}
	
	
	/**
	 * Sets the active Map.
	 * @param map The new active Map.
	 */
	public void setCurrentMap(Map map) {
		if(!mapList.contains(map))
			mapList.add(map);
		currentMap = map;
	}
	
	/**
	 * Returns the list of Maps.
	 * @return The list of Maps.
	 */
	public List<Map> getMapList() {
		
		return mapList;
	}
	
	
	/**
	 * Sets the map list for the World to the specified Map list.
	 * @param mapList The new list of Maps for the World.
	 */
	public void setMapList(ArrayList<Map> mapList) {
		this.mapList = mapList;
	}
	
	
	/**
	 * Returns the String representation of the World.
	 * @return The String representation of the World.
	 */
	public String toString() {
		
		return "World " + currentMap.getName();
	}
	
	
	/**
	 * Returns the Map with the specified name.
	 * @param mapName The name of the map to be retrieved.
	 * @return The Map with the specified name.
	 */
	public Map getMap(String mapName) {
		List<Map> mapList = this.getMapList();
		Map retMap = null;
		for(Map m: mapList) {
			String name = m.toString().substring(m.toString().lastIndexOf(File.separatorChar) + 1);
			if(name.equals(mapName)) {
				retMap = m;
				break;
			}
		}
		return retMap;
	}

}
